package com.banyiyi.simple.pattern.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {


    // 定义切点
    @Pointcut("@annotation(com.banyiyi.simple.pattern.annotation.LogAround)")
    public void point(){}


    @Before(value = "point()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("我是前置通知");
        String className = joinPoint.getSignature().getDeclaringType().getSimpleName();
        String method = joinPoint.getSignature().getName();
        System.out.printf("监听调用的方法%s,和此方法所对应的对象%s",method,className);
        System.out.println("-------------------------------------------------");
    }

    @After(value = "point()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("我是后置通知");
    }


    @Around(value = "point()")
    public Object doAround(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("我是回环通知");
        String method = pj.getSignature().getName();
        Object o = pj.proceed();
        System.out.printf("回环通知监听的方法%s,返回的结果为%s",method,o);
        return o;
    }

}
